<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.chezhujie.www.persistence.${simpleClassName!}Mapper">

    <select id="getList" parameterType="map" resultType="HashMap">
        <include refid="q"></include>

        <if test="endRows != null ">
            limit ${r"${"}startRows${r"}"},${r"${"}limitRows${r"}"}
        </if>
    </select>

    <select id="getTotalRows" parameterType="map" resultType="int">
        select count(tt.${keyProperties!}) from (
        <include refid="q"></include>
        ) tt

    </select>

    <insert id="insert" parameterType="${simpleClassName!}" >
        insert into ${tablename!}(
        <#list columnList as var>${var.name} <#if columnList?size-1 != var_index>,</#if></#list>
        )
        values (
        <#list columnList as var>${r"#{"}${var.properties}${r"}"}<#if columnList?size-1 != var_index>,</#if></#list>
        )
    </insert>

    <update id="update" parameterType="${simpleClassName!}" >
      update ${tablename!} t
        <set>
        <#list columnList as var>
           <#if var.properties != keyProperties>
                <if test="${var.properties}!=null">t.${var.name}=${r"#{"}${var.properties}${r"}"},</if>
           </#if>
        </#list>
        </set>
        where t.${primaryId!}=${r"#{"}${keyProperties}${r"}"}
    </update>

    <update id="deleteById" parameterType="string" >
        delete from ${tablename!}  where ${primaryId!}=${r"#{"}${keyProperties}${r"}"}
    </update>

    <update id="deleteByIds" parameterType="list" >
        delete from ${tablename!}  where ${primaryId!} in
        <foreach collection="list" open="(" close=")" index="index" separator="," item="item">
        ${r"#{item}"}
        </foreach>

    </update>

    <insert id="saves" parameterType="list" >
        INSERT INTO ${tablename!} (
    <#list columnList as var>${var.name} <#if columnList?size-1 != var_index>,</#if></#list>
        ) VALUES
        <foreach collection="list" item="item" index="index" open="" close="" separator=",">
            (
        <#list columnList as var>${r"#{item."}${var.properties}${r"}"}<#if columnList?size-1 != var_index>,</#if></#list>
            )

        </foreach>
    </insert>

    <select id="queryById" parameterType="string" resultType="${simpleClassName}">
        select
    <#list columnList as var>
        t.${var.name} as "${var.properties}"<#if columnList?size-1 != var_index>,</#if>
    </#list>
        from ${tablename!} t where t.${primaryId!}=${r"#{"}${keyProperties}${r"}"} limit 1
    </select>

    <select id="queryByIds" parameterType="list" resultType="${simpleClassName}">
        select
    <#list columnList as var>
        t.${var.name} as "${var.properties}"<#if columnList?size-1 != var_index>,</#if>
    </#list>
        from ${tablename!} t where t.${primaryId!} in
        <foreach collection="list" open="(" close=")" index="index" separator="," item="item">
            ${r"#{item}"}
        </foreach>
    </select>
<#list columnList as var>
    <#if 'select'==var.inputType&&('int'==var.inputCode||'str'==var.inputCode)>
    <select id="list${var.propertiesUp}" parameterType="map" resultType="HashMap">

    </select>
    </#if>
</#list>




    <sql id="q" >
        select
    <#list columnList as var>
        t.${var.name} as "${var.properties}"<#if columnList?size-1 != var_index>,</#if>
    </#list>
        from ${tablename!} t
        <where>
        <#list columnList as var>
            <#if var.properties != keyProperties>
                <#if var.classtype=='java.lang.String'>
                    <if test="${var.properties}!=null and ${var.properties}!=''">
                     and t.${var.name} like '%${r"${"}${var.properties}${r"}"}%'
                    </if>
                </#if>
                <#if var.classtype=='java.lang.Integer'>
                    <if test="${var.properties}!=null">
                        and t.${var.name} =${r"${"}${var.properties}${r"}"}
                    </if>
                </#if>
                <#if var.classtype=='java.math.BigDecimal'>
                    <if test="${var.properties}!=null">
                        and t.${var.name} =${r"#{"}${var.properties}${r"}"}
                    </if>
                </#if>
                <#if var.classtype=='java.util.Date'>
                    <if test="${var.properties}Start!=null ">
                        and t.${var.name} &gt;=${r"#{"}${var.properties}Start${r"}"}
                    </if>
                    <if test="${var.properties}End!=null ">
                        and t.${var.name} &lt;=${r"#{"}${var.properties}End${r"}"}
                    </if>
                </#if>

            </#if>
        </#list>
            ${r"<if"} test="orderby != null and orderby != ''">
                ORDER BY  ${r"${orderby}  ${order}"}
            ${r"</if>"}

        </where>

    </sql>


    <select id="getApiList" parameterType="map" resultType="HashMap">
        <include refid="apiq"></include>

        <if test="endRows != null ">
            limit ${r"${"}startRows${r"}"},${r"${"}limitRows${r"}"}
        </if>
    </select>

    <select id="getApiTotalRows" parameterType="map" resultType="int">
        select count(tt.${keyProperties!}) from (
        <include refid="apiq"></include>
        ) tt

    </select>
    <sql id="apiq" >
        select
    <#list columnList as var>
        t.${var.name} as "${var.properties}"<#if columnList?size-1 != var_index>,</#if>
    </#list>
        from ${tablename!} t
        <where>
        <#list columnList as var>
            <#if var.properties != keyProperties>
                <#if var.classtype=='java.lang.String'>
                    <if test="${var.properties}!=null and ${var.properties}!=''">
                        and t.${var.name} like '%${r"${"}${var.properties}${r"}"}%'
                    </if>
                </#if>
                <#if var.classtype=='java.lang.Integer'>
                    <if test="${var.properties}!=null">
                        and t.${var.name} =${r"${"}${var.properties}${r"}"}
                    </if>
                </#if>
                <#if var.classtype=='java.math.BigDecimal'>
                    <if test="${var.properties}!=null">
                        and t.${var.name} =${r"#{"}${var.properties}${r"}"}
                    </if>
                </#if>
                <#if var.classtype=='java.util.Date'>
                    <if test="${var.properties}Start!=null ">
                        and t.${var.name} &gt;=${r"#{"}${var.properties}Start${r"}"}
                    </if>
                    <if test="${var.properties}End!=null ">
                        and t.${var.name} &lt;=${r"#{"}${var.properties}End${r"}"}
                    </if>
                </#if>

            </#if>
        </#list>

        </where>

    </sql>
    <select id="getApiDetail" parameterType="map" resultType="HashMap">
        select
    <#list columnList as var>
        t.${var.name} as "${var.properties}"<#if columnList?size-1 != var_index>,</#if>
    </#list>
        from ${tablename!} t
        <where>
        <#list columnList as var>
            <#if var.properties != keyProperties>
                <#if var.classtype=='java.lang.String'>
                    <if test="${var.properties}!=null and ${var.properties}!=''">
                        and t.${var.name} like '%${r"${"}${var.properties}${r"}"}%'
                    </if>
                </#if>
                <#if var.classtype=='java.lang.Integer'>
                    <if test="${var.properties}!=null">
                        and t.${var.name} =${r"${"}${var.properties}${r"}"}
                    </if>
                </#if>
                <#if var.classtype=='java.math.BigDecimal'>
                    <if test="${var.properties}!=null">
                        and t.${var.name} =${r"#{"}${var.properties}${r"}"}
                    </if>
                </#if>
                <#if var.classtype=='java.util.Date'>
                    <if test="${var.properties}Start!=null ">
                        and t.${var.name} &gt;=${r"#{"}${var.properties}Start${r"}"}
                    </if>
                    <if test="${var.properties}End!=null ">
                        and t.${var.name} &lt;=${r"#{"}${var.properties}End${r"}"}
                    </if>
                </#if>

            </#if>
        </#list>

        </where>
        limit 1
    </select>
    <select id="queryByOne" parameterType="map" resultType="${simpleClassName}">
        select
    <#list columnList as var>
        t.${var.name} as "${var.properties}"<#if columnList?size-1 != var_index>,</#if>
    </#list>
        from ${tablename!} t
        <where>
        <#list columnList as var>
            <#if var.properties != keyProperties>
                <#if var.classtype=='java.lang.String'>
                    <if test="${var.properties}!=null and ${var.properties}!=''">
                        and t.${var.name} =${r"#{"}${var.properties}${r"}"}
                    </if>
                </#if>
                <#if var.classtype=='java.lang.Integer'>
                    <if test="${var.properties}!=null">
                        and t.${var.name} =${r"${"}${var.properties}${r"}"}
                    </if>
                </#if>
            </#if>
        </#list>

        </where>
        limit 1
    </select>
</mapper>